import { IVirtualRoomItem, IMedia, SaveVirtualRoom } from '@/api/sysConfig/virtualRoomConfig/interface'
import { IVirtualRoomListItem, VirtualRoomItemModel, IVirtualMeetingRoom, MediaVo } from './interface'
import { t } from 'i18next'

export function transVirtualRoomListItem(data: IVirtualRoomItem[]): IVirtualRoomListItem[] {
	return data.map((item) => ({
		id: item.vmr_moid,
		name: item.name,
		type: item.max_join_mt + t('方'),
		e164: item.e164,
		rate: item.conf_type === 2 ? getSfuBiterateText(item.video_formats[0]) : getBiterateText(item.video_formats[0]),
		exclusive: item.exclusive ? 'open' : 'close',
		exclusiveUser: item.exclusive_users?.map((user) => user.name).join(','),
		publicMeeting: item.public_conf ? 'open' : 'close',
		status: item.status === 1 ? 'inUse' : 'free',
	}))
}

export function transVirtualRoomDetail(data: IVirtualRoomItem | null, isSupportTranspond?: 0 | 1) {
	return {
		isH265:
			data?.video_formats[0]?.format === 6 ? 'H.265' : data?.conf_type === 2 ? 'H.264' : 'H.264' + t('及其它'),
		meetingType: [t('传统会议'), t('端口会议'), t('超元RTC会议')][data?.conf_type ?? 0],
		meetingProtocol: ['H323', 'SIP', 'RTC'][data?.conf_protocol ?? 0],
		encryptionType: [t('不加密'), '', t('AES加密'), t('国密加密')][data?.encrypted_type ?? 0],
		simpleCascade: data?.cascade_mode === 1 ? t('合并级联') : t('简单级联'),
		initMute: data?.mute ? t('开启') : t('关闭'),
		muteException: data?.mute_filter ? t('开启') : t('关闭'),
		autoImageComposition: data?.vmp?.mode === 2 ? t('开启') : t('关闭'),
		nMinusOneMode: data?.vmp?.except_self ? t('开启') : t('关闭'),
		intelligentRemix: data?.mix?.mode ? t('开启') : t('关闭'),
		enableVideo: data?.enable_video ? t('开启') : t('关闭'),
		enableLive: data?.enable_live ? t('开启') : t('关闭'),
		watermark: data?.watermark ? t('开启') : t('关闭'),
		enableDCS: data?.enable_dcs ? t('开启') : t('关闭'),
		dcsMode: data?.enable_dcs ? (data?.dcs_mode === 1 ? t('管理方控制') : t('自主协作')) : '',
		doubleFlowLimit: data?.dual_mode === 1 ? t('任意会场') : t('发言会场'),
		videoQuality: data?.video_quality === 1 ? t('速度优先') : t('质量优先'),
		voiceInspireEnable: data?.voice_activity_detection ? data?.vacinterval + 's' : t('关闭'),
		publicMeeting: data?.public_conf ? t('开启') : t('关闭'),
		oneReforming: data?.one_reforming ? t('开启') : t('关闭'),
		fecMode: data?.fec_mode ? t('开启') : t('关闭'),
		sourceUsed: isSupportTranspond
			? data?.preoccupy_resource === 1
				? t('资源预占')
				: t('纯转发会议')
			: [t('关闭'), t('开启'), t('纯转发会议')][data?.preoccupy_resource ?? 0],
		cascadeUpload: data?.cascade_upload ? t('开启上传') : t('关闭上传'),
		cascadeReturn: data?.cascade_return ? t('开启回传') : t('关闭回传'),
		multiStream: data?.multi_stream ? t('开启多流') : t('关闭多流'),
		callMode: data?.call_mode === 0 ? t('手动呼叫') : t('自动呼叫'),
		callInterval: data?.call_interval === 0 ? t('无') : data?.call_interval,
		callTimes: data?.call_times === 0 ? t('不限次数') : data?.call_times,
		mtType: data?.max_join_mt + t('方'),
		cascadeReturnPara: data?.cascade_return_para ?? 0,
		name: data?.name ?? '',
		vmrE164: data?.e164 ?? '',
		exclusive: data?.exclusive ? t('开启') : t('关闭'),
		exclusiveUser: data?.exclusive_users ?? [],
		visible: data?.visible ? t('开启') : t('关闭'),
		visibleUserDomains: data?.visible_user_domains ?? [],
	}
}

export function transVirtualRoomItem(param: IVirtualRoomItem): VirtualRoomItemModel {
	return {
		audioFormats: param.audio_formats,
		allowAnonymousMt: param.anonymous_mt ?? 0,
		autoImageComposition: param.vmp?.mode == 2 ? 1 : 0,
		banMtCancelMute: param.force_mute,
		callInterval: String(param.call_interval ?? 20),
		callMode: param.call_mode ?? 2,
		callTimes: String(param.call_times ?? 20),
		callTimesUnlimited: !!!param.call_times,
		cascadeReturn: !!param.cascade_return,
		cascadeReturnPara: String(param.cascade_return_para ?? 0),
		cascadeUpload: !!param.cascade_upload,
		dcsMode: param.dcs_mode,
		doubleFlowLimit: param.dual_mode ?? 1,
		e164: param.e164 ?? '',
		enableDCS: !!param.enable_dcs,
		enableLive: param.recorder.recorder_mode === 2 || param.recorder.recorder_mode === 3,
		enableRtc: param.enable_rtc || 0,
		enableVideo: param.recorder.recorder_mode === 1 || param.recorder.recorder_mode === 3,
		encryptedAuth: !!param.encrypted_auth,
		encryptedKey: param.encrypted_key ?? '',
		encryptedSM: param.encrypted_type == 4 ? true : false,
		encryptionType: param.encrypted_type == 4 ? 3 : param.encrypted_type ?? 3,
		exclusive: !!param.exclusive,
		exclusiveUser: param.exclusive_users.map((item) => ({
			account: item.account,
			accountType: item.account_type,
			name: item.name,
			portrait: item.portrait,
		})),
		fecMode: param.fec_mode ?? 0,
		id: param.vmr_moid,
		inExclusive: false, //不需要
		initMute: !!param.mute,
		intelligentRemix: param.mix?.mode == 1 ? true : false,
		liveNoLogin: !!param.recorder?.anonymous,
		livePassword: param.recorder?.live_password ?? '',
		frame: param.video_formats?.[0]?.frame,
		rate: param.video_formats?.[0]?.bitrate,
		resolution: param.video_formats?.[0]?.resolution,
		video: param.video_formats?.[0]?.format,
		meetingProtocol: param.conf_protocol ?? 0,
		meetingType: param.conf_type,
		multiStream: !!param.multi_stream,
		muteException: !!param.mute_filter,
		nMinusOneMode: param.vmp?.except_self ?? 0,
		name: param.name ?? '',
		oneReforming: !!param.one_reforming,
		password: '', //不需要
		publicMeeting: !!param.public_conf,
		quantumEncryption: param.quantum_encryption ?? 0,
		simpleCascade: param.cascade_mode ?? 1,
		sourceUsed: param.preoccupy_resource,
		startDoubleflow: false, //不需要
		status: param.status ?? 0,
		superMtNum: 0, //不需要
		type: {
			multi: param.max_join_mt,
			name: param.max_join_mt + t('方'),
		},
		userDomainMoid: 'dd2a43d3-5dd0-4c4e-8bb8-68dabbbdc541', //不需要
		videoDoubleflow: param.recorder?.dual_stream ?? 0,
		videoPublish: param.recorder?.publish_mode ?? 0,
		videoQuality: param.video_quality ?? 0,
		visible: !!param.visible,
		visibleUserDomains: param.visible_user_domains ?? [],
		vmrE164: param.e164,
		vmrMoid: param.vmr_moid,
		voiceInspireEnable: !!param.voice_activity_detection,
		voiceInspireTime: param.vacinterval ?? 0,
		vrsId: param.recorder?.vrs_id ?? '',
		vrsServerMoid: '', //不需要
		watermark: !!param.watermark,
	}
}

export function transSaveParams(param: IVirtualMeetingRoom): SaveVirtualRoom {
	return {
		anonymous_mt: param.allowAnonymousMt,
		audio_formats: param.audioFormats,
		bitrate: param.rate ?? 2048,
		call_interval: parseInt(param.callInterval),
		call_times: param.callTimesUnlimited ? 0 : parseInt(param.callTimes),
		call_mode: param.meetingType === 2 ? 2 : param.callMode,
		cascade_mode: param.meetingType === 2 ? 1 : param.simpleCascade,
		cascade_return: param.cascadeReturn ? 1 : 0,
		cascade_return_para: parseInt(param.cascadeReturnPara),
		cascade_upload: param.cascadeUpload ? 1 : 0,
		conf_protocol: param.meetingType === 2 ? 2 : param.meetingProtocol,
		conf_type: param.meetingType,
		dcs_mode: param.dcsMode,
		dual_mode: param.doubleFlowLimit,
		enable_rtc: param.meetingType === 2 ? 1 : param.enableRtc ? 1 : 0,
		enable_dcs: param.meetingType === 2 ? 0 : param.enableDCS ? 1 : 0,
		enable_live: param.enableLive ? 1 : 0,
		enable_video: param.enableVideo ? 1 : 0,
		encrypted_auth: param.encryptedAuth ? 1 : 0,
		encrypted_key: param.encryptedKey,
		encrypted_type: param.meetingType === 2 ? 2 : param.encryptedSM ? 4 : param.encryptionType,
		exclusive: param.exclusive ? 1 : 0,
		exclusive_users: param.exclusiveUser.map((item: any) => ({
			account: item.account,
			account_type: item.accountType,
			name: item.name,
			portrait: item.portrait,
		})),
		e164: param.vmrE164,
		fec_mode: param.meetingType === 2 ? 0 : param.fecMode,
		force_mute: param.banMtCancelMute,
		max_join_mt: param.type.multi,
		mix: {
			mode: param.intelligentRemix ? 1 : 0,
		},
		multi_stream: param.multiStream ? 1 : 0,
		mute: param.initMute ? 1 : 0,
		mute_filter: param.muteException ? 1 : 0,
		name: param.name,
		one_reforming: param.meetingType === 2 ? 0 : param.oneReforming ? 1 : 0,
		preoccupy_resource: param.meetingType === 2 ? 2 : param.sourceUsed,
		public_conf: param.publicMeeting ? 1 : 0,
		quantum_encryption: param.quantumEncryption,
		recorder: {
			vrs_id: param.vrsId,
			publish_mode: param.videoPublish,
			dual_stream: param.videoDoubleflow,
			anonymous: param.liveNoLogin ? 1 : 0,
			live_password: param.livePassword,
			recorder_mode: getRecorderMode(param.enableVideo, param.enableLive),
		},
		vacinterval: param.voiceInspireTime,
		video_formats: [
			{
				format: param.video,
				resolution: param.resolution,
				frame: param.frame,
				bitrate: param.rate ?? 2048,
			},
		],
		visible: param.visible ? 1 : 0,
		visible_user_domains: param.visibleUserDomains,
		video_quality: param.meetingType === 2 ? 0 : param.videoQuality,
		vmp: {
			except_self: param.nMinusOneMode,
			mode: param.autoImageComposition ? 2 : 0,
		},
		vmr_moid: param.vmrMoid,
		voice_activity_detection: param.meetingType === 2 ? 0 : param.voiceInspireEnable ? 1 : 0,
		watermark: param.watermark ? 1 : 0,
	}
}

export function getRecorderMode(enableVideo: boolean, enableLive: boolean) {
	if (enableVideo && enableLive) {
		return 3
	}
	if (enableVideo) {
		return 1
	}
	if (enableLive) {
		return 2
	}
	return 0
}

export function getSfuBiterateText(param: IMedia) {
	if (param.resolution === 13) {
		return t('全高清(4M 1080p@30fps)')
	} else {
		return t('高清(2M 720p@30fps)')
	}
}

export function getBiterateText(media: IMedia) {
	if (media.frame === 25 && media.bitrate === 192 && media.resolution === 14) {
		return t('流畅(192K WCIF@25fps)')
	} else if (media.frame === 25 && media.bitrate === 256 && media.resolution === 15) {
		return t('标清(256K W4CIF@25fps)')
	} else if (media.frame === 30 && media.bitrate === 2048 && media.resolution === 12) {
		return t('高清(2M 720p@30fps)')
	} else if (media.frame === 30 && media.bitrate === 2048 && media.resolution === 13) {
		return t('全高清(2M 1080p@30fps)')
	} else if (media.frame === 60 && media.bitrate === 4096 && media.resolution === 13) {
		return t('全高清(4M 1080p@60fps)')
	} else if (media.frame === 30 && media.bitrate === 4096 && media.resolution === 16) {
		return t('超高清(4M 4K@30fps)')
	} else if (media.frame === 60 && media.bitrate === 8192 && media.resolution === 16) {
		return t('超高清(8M 4K@60fps)')
	} else {
		return t('自定义') + `（${media.bitrate}Kbps）`
	}
}

export function getOptionValue(obj: MediaVo) {
	//流畅（192K WCIF@25fps）
	if (obj.frame === 25 && obj.rate === 192 && obj.resolution === 14) {
		return 1
	}
	//标清（256K W4CIF@25fps）
	if (obj.frame === 25 && obj.rate === 256 && obj.resolution === 15) {
		return 2
	}
	//高清（2M 720P@30fps）
	if (obj.frame === 30 && obj.rate === 2048 && obj.resolution === 12) {
		return 3
	}
	//全高清（2M 1080P@30fps）
	if (obj.frame === 30 && obj.rate === 2048 && obj.resolution === 13) {
		return 4
	}
	//全高清（4M 1080P@60fps）
	if (obj.frame === 60 && obj.rate === 4096 && obj.resolution === 13) {
		return 5
	}
	//超高清（4M 4K@30fps）
	if (obj.frame === 30 && obj.rate === 4096 && obj.resolution === 16) {
		return 6
	}
	//超高清（8M 4K@60fps）
	if (obj.frame === 60 && obj.rate === 8192 && obj.resolution === 16) {
		return 7
	}
	return 0
}
